package vn.cuhanh;

import vn.cuhanh.utils.HelperFunc;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

public class ConfirmSharing extends Activity {
	
	private static String GA_CATEGORY = "share";

	private String name;
	private String description;
	private String imageUrl;
	private int destination;
	private String userDisplayName;
	private String userName;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			setContentView(R.layout.confirm_sharing);
			getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
			setFullWidth();
			
			Bundle extra = getIntent().getExtras();
			name = extra.getString("vn.cuhanh.ConfirmSharing.name");
			description = extra.getString("vn.cuhanh.ConfirmSharing.description");
			imageUrl = extra.getString("vn.cuhanh.ConfirmSharing.imageUrl");
			destination = extra.getInt("vn.cuhanh.ConfirmSharing.destination");
			userDisplayName = extra.getString("vn.cuhanh.ConfirmSharing.userDisplayName");
			userName = extra.getString("vn.cuhanh.ConfirmSharing.userName");
			String strDestination = getDestinationStringFromCode(destination);
			
			((TextView)findViewById(R.id.confirm_sharing_destination)).setText(
					getResources().getString(R.string.confirm_sharing_destination)
						.replace("destination", strDestination));
			((TextView)findViewById(R.id.confirm_sharing_message)).setText(
					getResources().getString(R.string.confirm_sharing_message)
						.replace("destination", strDestination)
						.replace("user_display_name", userDisplayName == null ? "" : userDisplayName)
						.replace("user_name", userName == null ? "" : userName));
			((TextView)findViewById(R.id.confirm_sharing_name)).setText(name);
			((TextView)findViewById(R.id.confirm_sharing_description)).setText(description);
			ImageView image = (ImageView)findViewById(R.id.confirm_sharing_image);
			UrlImageViewHelper.setUrlDrawable(image, imageUrl);
			Button btnOk = (Button)findViewById(R.id.confirm_sharing_btn_ok);
			btnOk.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					HelperFunc.trackEvent(ConfirmSharing.this, GA_CATEGORY, "btn_ok_press", "btn_ok_press");
					Intent data = new Intent();
					data.putExtra("result", ShareActivity.CONFIRM_SHARING_OK);
					data.putExtra("destination", destination);
					setResult(RESULT_OK, data);
					finish();
				}
			});
			Button btnLoginAnotherAccount = (Button)findViewById(R.id.confirm_sharing_btn_login_another_account);
			if(destination == ShareActivity.DESTINATION_FACEBOOK) {
				btnLoginAnotherAccount.setVisibility(View.GONE);
			}
			btnLoginAnotherAccount.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					HelperFunc.trackEvent(ConfirmSharing.this, GA_CATEGORY, "btn_login_another_account_press", "btn_login_another_account_press");
					Intent data = new Intent();
					data.putExtra("result", ShareActivity.CONFIRM_SHARING_LOGIN_ANOTHER_ACCOUNT);
					data.putExtra("destination", destination);
					setResult(RESULT_OK, data);
					finish();
				}
			});
			Button btnClose = (Button)findViewById(R.id.confirm_sharing_btn_cancel);
			btnClose.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					HelperFunc.trackEvent(ConfirmSharing.this, GA_CATEGORY, "btn_close_press", "btn_close_press");
					Intent data = new Intent();
					data.putExtra("result", ShareActivity.CONFIRM_SHARING_CANCEL);
					data.putExtra("destination", destination);
					setResult(RESULT_OK, data);
					finish();
				}
			});
		} catch (Exception e) {
			// TODO: handle exception
			HelperFunc.readStackTrace(e);
		}
	}
	
	public void setFullWidth() {
		android.view.WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = android.view.WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
	}

	public String getDestinationStringFromCode(int destination2) {
		// TODO Auto-generated method stub
		switch (destination2) {
		case ShareActivity.DESTINATION_ZING_ME:
			return getResources().getString(R.string.zingme);
		case ShareActivity.DESTINATION_FACEBOOK:
			return getResources().getString(R.string.facebook); 
		default:
			break;
		}
		return null;
	}
}
